package com.wangshuo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshuo.common.JwtHelper;
import com.wangshuo.config.Result;
import com.wangshuo.config.ResultCodeEnum;
import com.wangshuo.entity.Acount;
import com.wangshuo.entity.Admin;
import com.wangshuo.service.AdminService;
import com.wangshuo.mapper.AdminMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author 13326
* @description 针对表【admin】的数据库操作Service实现
* @createDate 2025-03-05 09:35:37
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{
    @Resource
    private JwtHelper jwtHelper;
    @Resource
    private AdminMapper adminMapper;
    @Override
    public Result login(Acount acount){
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,acount.getUsername());
        Admin admin=this.getOne(queryWrapper);

        if(null==admin){

        return Result.build(500, ResultCodeEnum.USERNAME_ERROR);
        }else if(!admin.getPassword().equals(acount.getPassword())){

                return Result.build(500,ResultCodeEnum.PASSWORD_ERROR);
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("token",jwtHelper.createToken(Long.valueOf(admin.getId())));
         return Result.ok(map);

    }
    @Override
    public Result register(Acount acount){
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,acount.getUsername());
        Admin admin =this.getOne(queryWrapper);
        if(null!=admin){
            return Result.build(500,ResultCodeEnum.USERNAME_USED);
        }else{
            Admin admin1=new Admin();
            admin1.setUsername(acount.getUsername());
            admin1.setPassword(acount.getPassword());
            admin1.setRole(acount.getRole());
            adminMapper.insert(admin1);
        }
        return Result.ok("成功");
    }
    @Override
    public IPage<Admin> getpage(IPage<Admin> page, String keyword){
        return adminMapper.selectpage(page,keyword);

    }

}




